﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AioNightly.Model;
using MySql.Data.MySqlClient;
using AioNightly.Common;

namespace AioNightly.Dao
{
    public class ItemDAO
    {
        private void Read2List(MySqlDataReader reader, List<Item> list)
        {
            while (reader.Read())
            {
                Item it = new Item();
                it.Id = reader.GetInt32(0);
                it.Type = reader.GetInt32(1);
                it.Content = reader.GetString(2);
                it.AuthorId = reader.GetInt32(3);
                it.CreateTime = reader.GetString(4);
                it.State = reader.GetInt32(5);
                it.BuildId = reader.GetInt32(6);

                list.Add(it);
            }
        }

        public List<Item> GetItemByRelease(int type, string releaseDate)
        {
            DBConnect db = new DBConnect();
            db.OpenConnection();

            List<Item> list = new List<Item>();
            string sql = string.Format("SELECT  * FROM item,build where item.Type={0} and item.BuildId = build.Id and  (build.BuildTime = '{1}')", type, releaseDate);
            MySqlCommand cmd = new MySqlCommand(sql, db.Conn);
            MySqlDataReader reader = cmd.ExecuteReader();

            Read2List(reader, list);

            if (reader != null)
                reader.Close();
            db.CloseConnection();

            return list;
        }

        public Item GetItemByID(int id)
        {
            DBConnect db = new DBConnect();
            db.OpenConnection();

            List<Item> list = new List<Item>();
            string sql = string.Format("SELECT  * FROM item where item.Id={0}", id);
            MySqlCommand cmd = new MySqlCommand(sql, db.Conn);
            MySqlDataReader reader = cmd.ExecuteReader();

            Read2List(reader, list);

            if (reader != null)
                reader.Close();
            db.CloseConnection();

            if (list.Count == 1)
            {
                return list[0];
            }
            else
            {
                return null;
            }
        }

        public int EditItemContent(int id, string content, int state)
        {
            DBConnect db = new DBConnect();
            db.OpenConnection();

            string sql = string.Format("update item set Content='{0}', State={2}  where id={1}", content, id, state);
            MySqlCommand cmd = new MySqlCommand(sql, db.Conn);
            int result = cmd.ExecuteNonQuery();

            return result;
        }

        public int DelItemById(int id)
        {
            DBConnect db = new DBConnect();
            db.OpenConnection();

            string sql = string.Format("delete from item where Id={0}", id);
            MySqlCommand cmd = new MySqlCommand(sql, db.Conn);
            int result = cmd.ExecuteNonQuery();

            return result;
        }

        public int AddNewItem(int type, string content)
        {
            DBConnect db = new DBConnect();
            db.OpenConnection();

            string sql = string.Format("insert into item (Type, Content, AuthorId, CreateTime, State, BuildId) values ({0}, '{1}', {2}, '{3}', 0, 0)", type, content, UserManager.GetCurrentUserId(), TimeManager.GetSystemTime());
            MySqlCommand cmd = new MySqlCommand(sql, db.Conn);
            int result = cmd.ExecuteNonQuery();

            return result;
        }

        public int LabelItem2Build(int buildId)
        {
            DBConnect db = new DBConnect();
            db.OpenConnection();

            string sql = string.Format("update item set buildid={0} where buildid is null", buildId);
            MySqlCommand cmd = new MySqlCommand(sql, db.Conn);
            int result = cmd.ExecuteNonQuery();

            return result;
        }
    }
}